package com.education.welco.controller.app;


import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.*;
import com.education.welco.service.ISCatalogMenuService;
import com.education.welco.service.ISCatalogService;
import com.education.welco.service.ISQuestionsStuService;
import com.education.welco.service.impl.SCatalogServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@Api(tags = {"APP-学校课程--提问"})
@RestController
@RequestMapping("/app/sQuestionsStu")
public class AppSQuestionsStuController {

    @Autowired
    private ISQuestionsStuService questionsStuService;

    @Autowired
    private ISCatalogService isCatalogService;

    @Autowired
    private ISCatalogMenuService catalogMenuService;


    @ApiOperation(value = "学校课程--提问接口", httpMethod = "POST")
    @PostMapping("/insertByQuestions")
    public ApiReturnObject insertByQuestions(SQuestionsStu sQuestionsStu) {
        if ("".equals(sQuestionsStu.getQuestionContent()) || sQuestionsStu.getQuestionContent() == null
                || sQuestionsStu.getCatalogId() == null
                || sQuestionsStu.getStudentId() == null
                || sQuestionsStu.getTeacherId() == null){
            return new ApiReturnObject("201", "参数缺失", "参数缺失");
        }else {
            sQuestionsStu.setCreateTime(new Date());
            sQuestionsStu.setUserId(sQuestionsStu.getStudentId());
            sQuestionsStu.setIsDisplay("0");//默认 0 未回复  1 已回复
            questionsStuService.insertSQuestionsStu(sQuestionsStu);
            return new ApiReturnObject("200","提问成功","提问成功");
        }
    }

    @ApiOperation(value = "我的提问接口", httpMethod = "GET")
    @GetMapping("/selectByUserId")
    public ApiReturnObject selectByUserId(Integer userId) {
        if (userId == null){
            return new ApiReturnObject("201", "userId为空", "userId为空");
        }
        SQuestionsStu sQuestionsStu = new SQuestionsStu();
        sQuestionsStu.setUserId(userId);
        List<SQuestionsStu> questionsStuList = questionsStuService.selectSQuestionsStuList(sQuestionsStu);
        for (SQuestionsStu questionsStu : questionsStuList) {
            if (questionsStu.getCatalogId() == null){
                questionsStu.setCatalogName("来自xxx课程的基础知识");
            }else {
//                SCatalog catalog = isCatalogService.selectSCatalogById(questionsStu.getCatalogId());
                //关联catalogMenu表进行查询
                SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(questionsStu.getCatalogId());
                if (sCatalogMenu != null){
                    questionsStu.setCatalogName("来自" + sCatalogMenu.getName() + "的基础知识");
                }else {
                    questionsStu.setCatalogName("来自xxx课程的基础知识");
                }
            }
        }
        return new ApiReturnObject("200", "查询成功", questionsStuList.size(),questionsStuList);
    }
}
